<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="锁imei" prop="nodeId">
        <el-input
          v-model="queryParams.nodeId"
          placeholder="请输入锁imei"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="锁名称" prop="lockName">
        <el-input
          v-model="queryParams.lockName"
          placeholder="请输入锁名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="锁电量" prop="electric">
        <el-input
          v-model="queryParams.electric"
          placeholder="请输入锁电量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
 -->
      <el-form-item label="锁类型" prop="lockType">
        <el-select v-model="queryParams.lockType" placeholder="请选择锁类型" clearable size="small">
          <el-option label="公寓" value="1"/>
          <el-option label="学校" value="2"/>
        </el-select>
      </el-form-item>

      <el-form-item label="绑定状态" prop="startus">
        <el-select v-model="queryParams.startus" placeholder="请选择绑定状态" clearable size="small">
          <el-option label="已绑定" value="1"/>
          <el-option label="未绑定" value="0"/>
        </el-select>
      </el-form-item>

      <el-form-item label="连接时间" prop="connectTime">
        <!-- <el-date-picker clearable size="small"
                        v-model="queryParams.connectTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择连接时间">
        </el-date-picker> -->
         <el-date-picker
      v-model="queryParams.connectTime"
      type="datetime"
      placeholder="选择日期时间">
    </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出
        </el-button>
      </el-col>
	  <el-col :span="1.5">
	    <el-button
	      type="danger"
	      plain
	      icon="el-icon-delete"
	      size="mini"
	      @click="handleRefresh"
	      v-hasPermi="['system:info:edit']"
		  v-loading.fullscreen.lock="fullscreenLoading"
	    >同步设备
	    </el-button>
	  </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 显示 -->
    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="连接时间" align="center" prop="id" v-if="false"/>
      <el-table-column label="imei" align="center" prop="nodeId"/>
      <!-- <el-table-column label="token" align="center" prop="lockToken"/> -->
      <!-- <el-table-column label="商家编号" align="center" prop="merNo"/> -->
      <el-table-column label="名称" align="center" prop="lockName"/>
      <el-table-column label="地址" align="center" prop="lockAddress"/>
      <!-- <el-table-column label="经度" align="center" prop="longitude"/>
      <el-table-column label="纬度" align="center" prop="latitude"/> -->
      <el-table-column label="信号" align="center" prop="signal"/>
      <el-table-column label="电量" align="center" prop="electric"/>
      <el-table-column label="类型" align="center" prop="lockType">
        <template slot-scope="scope">
          <span v-if="scope.row.lockType==1">公寓</span>
          <span v-else-if="scope.row.lockType==2">学校</span>

        </template>
      </el-table-column>

    <!--  <el-table-column label="锁类型" align="center" prop="appType"/> -->
     <!-- <el-table-column label="注册平台" align="center" prop="platType"/> -->

    
      <el-table-column label="注册状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.startus"
            :active-value="1"
            :inactive-value="0"
            @change="handleResgisterChange(scope.row)"
          ></el-switch>

        </template>
      </el-table-column>

      <el-table-column label="连接时间" align="center" prop="connectTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.connectTime) }}</span>
        </template>
      </el-table-column>
     <!-- <el-table-column label="创建人" align="center" prop="remark"/> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
			
            v-hasPermi="['system:info:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
			v-if="scope.row.startus"
            icon="el-icon-zoom-in"
            @click="handleWatch(scope.row)"
            v-hasPermi="['system:info:remove']"
          >查看
          </el-button>
        <el-button
            size="mini"
            type="text"
			v-if="scope.row.startus"
            icon="el-icon-switch-button"
            @click="handleStartLock(scope.row)"
            v-hasPermi="['system:info:remove']"
          >开锁
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增或修改弹出框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="锁imei" prop="nodeId">
          <el-input v-model="form.nodeId" placeholder="请输入锁imei"/>
        </el-form-item>
        <el-form-item label="锁名称" prop="lockName">
          <el-input v-model="form.lockName" placeholder="请输入锁名称"/>
        </el-form-item>
        <el-form-item label="锁地址" prop="lockAddress">
          <el-input v-model="form.lockAddress" placeholder="请输入锁地址"/>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度"/>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度"/>
        </el-form-item>
        <el-form-item label="锁类型" prop="lockType">
          <el-select v-model="form.lockType" placeholder="请选择锁类型 1公寓 2学校">
            <el-option label="公寓" value="1"/>
            <el-option label="学校" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder=""/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看弹出框 -->
    <el-dialog :title="title" :visible.sync="openView" width="700px" append-to-body>
      <el-form :label-position="labelPosition" ref="form" :model="form" label-width="120px" size="mini"
              align-item="left">
        <el-row>
          <el-col :span="12">
            <el-form-item label="imei:">{{ form.nodeId }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="token:">{{ form.lockToken }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商家编号:">{{ form.merNo }}</el-form-item>
            <el-form-item label="名称:">{{ form.lockName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址:">{{ form.lockAddress }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经度:">{{ form.longitude }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度:">{{ form.latitude }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信号:">{{ form.signal }}</el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="电量:">{{ form.electric }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型:">
              <div v-if="form.lockType == 1">公寓</div>
              <div v-else-if="form.lockType == 2">学校</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型:">{{ form.appType }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册平台:">{{ form.platType }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册状态:">
              <div v-if="form.startus == 0">未注册</div>
              <div v-else-if="form.startus == 1">已注册</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="连接时间:">{{ form.connectTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间:">{{ form.createTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人:">{{ form.createBy }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修改时间:">{{ form.updateTime }}</el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="修改人:">{{ form.updateBy }}</el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
          <el-form-item label="备注:">

          </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  exportInfo,
  registerInfo,
  getLockInfo,
  refreshLock,
  startLock
} from "@/api/lock/lockInfo";

export default {
  name: "Info",
  components: {},
  data() {
    return {
	  fullscreenLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看弹出层
      openView: false,
      // 表单域标签位置
      labelPosition: "left",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nodeId: undefined,
        lockToken: undefined,
        merNo: undefined,
        lockName: undefined,
        lockAddress: undefined,
        longitude: undefined,
        latitude: undefined,
        signal: undefined,
        electric: undefined,
        lockType: undefined,
        appType: undefined,
        platType: undefined,
        startus: undefined,
        connectTime: undefined,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        platType: [
          {required: true, message: "注册到某平台不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {

        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
        // console.log(response);

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nodeId: undefined,
        lockToken: undefined,
        merNo: undefined,
        lockName: undefined,
        lockAddress: undefined,
        longitude: undefined,
        latitude: undefined,
        signal: undefined,
        electric: undefined,
        lockType: undefined,
        appType: undefined,
        platType: undefined,
        startus: undefined,
        connectTime: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      // this.$refs.queryForm.resetFields()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
	handleStartLock(row){
		console.log(row);
		startLock(row.id).then((response)=>{
			console.log(response)
			if(response.code==200){
				 this.msgSuccess("开锁成功");
			}
		})
	},
    //查看按钮
    handleWatch(row) {
      getInfo(row.id).then(response => {
        this.form = response.data;
        this.openView = true;
        this.title = "锁信息";
      })
    },
	//刷新同步锁
	handleRefresh(){
		this.fullscreenLoading = true;
		refreshLock().then((response)=>{
			this.fullscreenLoading = false;
			})
	},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();

            })
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportInfo(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    // 注册状态修改
    handleResgisterChange(row) {
      let text = row.startus === 0 ? "取消注册" : "注册";
      this.$confirm('确认要"' + text + '"锁吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return registerInfo(row.id);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function () {
        if (row.startus == 0) {
          row.startus = 1
        } else if (row.startus == 1) {
          row.startus = 0
        }
      });


    },
  },

};
</script>
